part of 'table.dart';

class Thought implements Identity {
  @override
  final int id;

  /// 想法产生的时间
  DateTime timestamp;
  String content;

  Thought({required this.id, required this.timestamp, required this.content});

  factory Thought.fromRaw(Json raw) {
    final timestampRaw = raw["timestamp"];
    late DateTime timestamp;

    if (timestampRaw is DateTime) {
      timestamp = timestampRaw.toLocal();
    } else if (timestampRaw is String) {
      // 从json中传输的时候，dateTime又是String格式，注意本地化
      timestamp = DateTime.parse(timestampRaw).toLocal();
    } else {
      throw BugDev("未知的Thought想法中的timestamp: ${timestampRaw.runtimeType}");
    }

    return Thought(
      id: raw["id"],
      timestamp: timestamp,
      content: raw["content"],
    );
  }

  @override
  Json toJson() => {
        "id": id,
        "timestamp": timestamp.toString(),
        "content": content,
      };
}
